﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace JinYuan.VirtualDataLibrary
{
    public partial class MsgshowTip : Form
    {
        /// <summary>
        /// 图标和文本之间的间距（像素）
        /// </summary>
        const int IconTextSpacing = 3;
        /// <summary>
        /// 显示文字
        /// </summary>
        string _tipText;
        /// <summary>
        /// 提示图标
        /// </summary>
        public Image TipIcon { get; set; }

        /// <summary>
        /// 提示文本
        /// </summary>
        public string TipText
        {
            get { return _tipText ?? string.Empty; }
            set { _tipText = value; }
        }

        public MsgshowTip(string Msg)
        {
            //双缓冲。有必要
            SetStyle(ControlStyles.UserPaint, true);
            DoubleBuffered = true;
            InitializeComponent();
            this._tipText = Msg;
        }

        private void MsgshowTip_Load(object sender, EventArgs e)
        {
            //ProcessClientSize();
            ProcessLocation();
            label1.Text = _tipText;
            timer1.Enabled = true;
        }

        /// <summary>
        /// 根据图标和文字处理窗体尺寸
        /// </summary>
        private void ProcessClientSize()
        {
            Size size = Size.Empty;
            if (TipIcon != null)
            {
                size += TipIcon.Size;
            }
            if (TipText.Length != 0)
            {
                if (TipIcon != null)
                {
                    size.Width += IconTextSpacing;//图标与文字的间距
                }
                var textSize = TextRenderer.MeasureText(TipText, this.Font);
                size.Width += textSize.Width;
                if (size.Height < textSize.Height) { size.Height = textSize.Height; }
            }
            this.ClientSize = size + Padding.Size;
        }
        private int x, y;  //显示的坐标变量

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ProcessLocation()
        {

            #region 弹窗显示在屏幕中间
            this.y = (Screen.PrimaryScreen.Bounds.Height - 30 - this.Height) / 2;
            this.x = (Screen.PrimaryScreen.Bounds.Width - 10 - this.Width) / 2;
            this.Location = new Point(this.x, this.y);
            #endregion

            #region 弹窗显示在屏幕右下角
            //this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width + 15;
            //this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height * i - 5 * i;
            //this.Location = new Point(this.x, this.y);
            #endregion

            #region 弹窗跟随鼠标位置
            //var p = BasePoint;
            //p.X -= Screen.PrimaryScreen.WorkingArea.Width - this.Width - 1000;//Screen.PrimaryScreen.WorkingArea.Width-this.Width / 2;

            ////横向处理。距离屏幕左右两边太近时的处理
            //int screenWidth;
            //if (p.X < 10)
            //{
            //    p.X = 10;
            //}
            //else if (p.X + this.Width > (screenWidth = Screen.PrimaryScreen.Bounds.Width) - 10)
            //{
            //    p.X = screenWidth - 10 - this.Width;
            //}

            ////纵向处理。在鼠标上方显示
            //p.Y -= this.Height + 20;

            //this.Location = p;
            #endregion
        }

    }
}
